#!/bin/bash

# postinst script for driver-network-cxgbe
#
# see: dh_installdeb(1)

#set -e

# summary of how this script can be called:
#        * <postinst> \`configure' <most-recently-configured-version>
#        * <old-postinst> \`abort-upgrade' <new version>
#        * <conflictor's-postinst> \`abort-remove' \`in-favour' <package>
#          <new-version>
#        * <postinst> \`abort-remove'
#        * <deconfigured's-postinst> \`abort-deconfigure' \`in-favour'
#          <failed-install-package> <version> \`removing'
#          <conflicting-package> <version>
# for details, see http://www.debian.org/doc/debian-policy/ or
# the debian-policy package

PATH=/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/sbin
export PATH

if [ "${BASEDIR:=/}" = "/" ]; then
    BASEDIR=""
else
    BASEDIR_OPT="-b $BASEDIR"
fi

case "$1" in
    configure)
        chown root:sys $BASEDIR/kernel/firmware
	chown root:sys $BASEDIR/kernel/firmware/cxgbe
	if [ "$ZONEINST" = "1" ]; then
	    [ -f $BASEDIR/usr/sbin/cxgbetool ] && rm -f $BASEDIR/usr/sbin/cxgbetool
	else
	    chmod 0555 "$BASEDIR/usr/sbin/cxgbetool"
	    chown root:bin "$BASEDIR/usr/sbin/cxgbetool"
	fi
	if [ "$ZONEINST" = "1" ]; then
	    [ -f $BASEDIR/kernel/firmware/cxgbe/t4fw-1.27.1.0.bin ] && rm -f $BASEDIR/kernel/firmware/cxgbe/t4fw-1.27.1.0.bin
	else
	    chmod 0644 "$BASEDIR/kernel/firmware/cxgbe/t4fw-1.27.1.0.bin"
	    chown root:bin "$BASEDIR/kernel/firmware/cxgbe/t4fw-1.27.1.0.bin"
	fi
	if [ "$ZONEINST" = "1" ]; then
	    [ -f $BASEDIR/kernel/firmware/cxgbe/t4fw_cfg.txt ] && rm -f $BASEDIR/kernel/firmware/cxgbe/t4fw_cfg.txt
	else
	    [ -f $BASEDIR/kernel/firmware/cxgbe/t4fw_cfg.txt ] || cp -ax $BASEDIR/usr/share/doc/driver-network-cxgbe/examples/t4fw_cfg.txt $BASEDIR/kernel/firmware/cxgbe/t4fw_cfg.txt
	    chmod 0644 "$BASEDIR/kernel/firmware/cxgbe/t4fw_cfg.txt"
	    chown root:bin "$BASEDIR/kernel/firmware/cxgbe/t4fw_cfg.txt"
	fi
	if [ "$ZONEINST" = "1" ]; then
	    [ -f $BASEDIR/kernel/firmware/cxgbe/t5fw-1.27.1.0.bin ] && rm -f $BASEDIR/kernel/firmware/cxgbe/t5fw-1.27.1.0.bin
	else
	    chmod 0644 "$BASEDIR/kernel/firmware/cxgbe/t5fw-1.27.1.0.bin"
	    chown root:bin "$BASEDIR/kernel/firmware/cxgbe/t5fw-1.27.1.0.bin"
	fi
	if [ "$ZONEINST" = "1" ]; then
	    [ -f $BASEDIR/kernel/firmware/cxgbe/t5fw_cfg.txt ] && rm -f $BASEDIR/kernel/firmware/cxgbe/t5fw_cfg.txt
	else
	    [ -f $BASEDIR/kernel/firmware/cxgbe/t5fw_cfg.txt ] || cp -ax $BASEDIR/usr/share/doc/driver-network-cxgbe/examples/t5fw_cfg.txt $BASEDIR/kernel/firmware/cxgbe/t5fw_cfg.txt
	    chmod 0644 "$BASEDIR/kernel/firmware/cxgbe/t5fw_cfg.txt"
	    chown root:bin "$BASEDIR/kernel/firmware/cxgbe/t5fw_cfg.txt"
	fi
	if [ "$ZONEINST" = "1" ]; then
	    [ -f $BASEDIR/kernel/firmware/cxgbe/t6fw-1.27.1.0.bin ] && rm -f $BASEDIR/kernel/firmware/cxgbe/t6fw-1.27.1.0.bin
	else
	    chmod 0644 "$BASEDIR/kernel/firmware/cxgbe/t6fw-1.27.1.0.bin"
	    chown root:bin "$BASEDIR/kernel/firmware/cxgbe/t6fw-1.27.1.0.bin"
	fi
	if [ "$ZONEINST" = "1" ]; then
	    [ -f $BASEDIR/kernel/firmware/cxgbe/t6fw_cfg.txt ] && rm -f $BASEDIR/kernel/firmware/cxgbe/t6fw_cfg.txt
	else
	    [ -f $BASEDIR/kernel/firmware/cxgbe/t6fw_cfg.txt ] || cp -ax $BASEDIR/usr/share/doc/driver-network-cxgbe/examples/t6fw_cfg.txt $BASEDIR/kernel/firmware/cxgbe/t6fw_cfg.txt
	    chmod 0644 "$BASEDIR/kernel/firmware/cxgbe/t6fw_cfg.txt"
	    chown root:bin "$BASEDIR/kernel/firmware/cxgbe/t6fw_cfg.txt"
	fi
	if [ "$ZONEINST" = "1" ]; then
	    [ -f $BASEDIR/kernel/drv/amd64/cxgbe ] && rm -f $BASEDIR/kernel/drv/amd64/cxgbe
	else
	    chmod 0755 "$BASEDIR/kernel/drv/amd64/cxgbe"
	    chown root:sys "$BASEDIR/kernel/drv/amd64/cxgbe"
	fi
	if [ "$ZONEINST" = "1" ]; then
	    [ -f $BASEDIR/kernel/drv/amd64/t4nex ] && rm -f $BASEDIR/kernel/drv/amd64/t4nex
	else
	    chmod 0755 "$BASEDIR/kernel/drv/amd64/t4nex"
	    chown root:sys "$BASEDIR/kernel/drv/amd64/t4nex"
	fi
	[ "$ZONEINST" = "1" ] || (grep -c "^t4nex " $BASEDIR/etc/name_to_major >/dev/null || ( add_drv -n  $BASEDIR_OPT  -i '"pciex1425,4400" "pciex1425,4401" "pciex1425,4402" "pciex1425,4403" "pciex1425,4404" "pciex1425,4405" "pciex1425,4406" "pciex1425,4407" "pciex1425,4408" "pciex1425,4409" "pciex1425,440a" "pciex1425,440d" "pciex1425,440e" "pciex1425,5400" "pciex1425,5401" "pciex1425,5402" "pciex1425,5403" "pciex1425,5404" "pciex1425,5405" "pciex1425,5406" "pciex1425,5407" "pciex1425,5408" "pciex1425,5409" "pciex1425,540a" "pciex1425,540b" "pciex1425,540c" "pciex1425,540d" "pciex1425,540e" "pciex1425,540f" "pciex1425,5410" "pciex1425,5411" "pciex1425,5412" "pciex1425,5413" "pciex1425,5414" "pciex1425,5415" "pciex1425,5416" "pciex1425,5417" "pciex1425,5418" "pciex1425,5480" "pciex1425,5481" "pciex1425,5482" "pciex1425,5486" "pciex1425,5487" "pciex1425,5488" "pciex1425,5489" "pciex1425,5490" "pciex1425,5491" "pciex1425,5492" "pciex1425,5493" "pciex1425,5494" "pciex1425,5495" "pciex1425,5496" "pciex1425,5497" "pciex1425,5498" "pciex1425,5499" "pciex1425,549a" "pciex1425,549b" "pciex1425,549c" "pciex1425,549d" "pciex1425,549e" "pciex1425,549f" "pciex1425,54a0" "pciex1425,54a1" "pciex1425,54a2" "pciex1425,54a3" "pciex1425,54a4" "pciex1425,54a5" "pciex1425,54a6" "pciex1425,54a7" "pciex1425,54a8" "pciex1425,54a9" "pciex1425,54aa" "pciex1425,54ab" "pciex1425,54ac" "pciex1425,54ad" "pciex1425,54ae" "pciex1425,54af" "pciex1425,54b0" "pciex1425,6400" "pciex1425,6401" "pciex1425,6402" "pciex1425,6403" "pciex1425,6404" "pciex1425,6405" "pciex1425,6406" "pciex1425,6407" "pciex1425,6408" "pciex1425,6409" "pciex1425,640d" "pciex1425,6410" "pciex1425,6411" "pciex1425,6414" "pciex1425,6415" "pciex1425,6480" "pciex1425,6481" "pciex1425,6482" "pciex1425,6483" "pciex1425,6484" "pciex1425,6485" "pciex1425,6486" "pciex1425,6487" "pciex1425,6488" "pciex1425,6489" "pciex1425,648a" "pciex1425,648b" "pciex1425,6492"' -m '* 0666 root sys' t4nex ) )
	[ "$ZONEINST" = "1" ] || (grep -c "'t4nex 0666 root sys'" $BASEDIR/etc/minor_perm >/dev/null || update_drv -n  -a $BASEDIR_OPT  -m 't4nex 0666 root sys' clone)
	[ "$ZONEINST" = "1" ] || (grep -c "^type=ddi_ctl:devctl;minor1=t4nex" $BASEDIR/etc/devlink.tab >/dev/null || (echo "type=ddi_ctl:devctl;minor1=t4nex	t4nex\M2" >> $BASEDIR/etc/devlink.tab))
	[ "$ZONEINST" = "1" ] || (grep -c "^cxgbe " $BASEDIR/etc/name_to_major >/dev/null || ( add_drv -n  $BASEDIR_OPT  -m '* 0666 root sys' cxgbe ) )
	[ "$ZONEINST" = "1" ] || (grep -c "'cxgbe 0666 root sys'" $BASEDIR/etc/minor_perm >/dev/null || update_drv -n  -a $BASEDIR_OPT  -m 'cxgbe 0666 root sys' clone)
	
	# [ "$ZONEINST" = "1" ] || (mkdir -p $BASEDIR/kernel/firmware/cxgbe && ln -f -s t4fw-1.27.1.0.bin $BASEDIR/kernel/firmware/cxgbe/t4fw.bin)
	# [ "$ZONEINST" = "1" ] || (mkdir -p $BASEDIR/kernel/firmware/cxgbe && ln -f -s t5fw-1.27.1.0.bin $BASEDIR/kernel/firmware/cxgbe/t5fw.bin)
	# [ "$ZONEINST" = "1" ] || (mkdir -p $BASEDIR/kernel/firmware/cxgbe && ln -f -s t6fw-1.27.1.0.bin $BASEDIR/kernel/firmware/cxgbe/t6fw.bin)
    ;;

    abort-upgrade|abort-remove|abort-deconfigure)
    ;;

    *)
        echo "postinst called with unknown argument '$1'" >&2
        exit 1
    ;;
esac



# dh_installdeb will replace this with shell code automatically
# generated by other debhelper scripts.

#DEBHELPER#

exit 0

